package weaver.interfaces.sd;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

/**
 * 该定时器用于查询企画模块中各个环节的进度及提示用户去填写对应的阶段性报告
 * 提醒方式为流程提醒 前三天 前两天 前一天
 * @author Administrator
 *
 */
public class WorkflowRemindCron extends BaseCronJob{
	private static final int FIRST = 1;	//流程前一天提醒
	private static final int SECOND = 2;//流程前两天提醒
	private static final int THRID = 3;	//流程前三天提醒

	private static BaseBean baseBean = new BaseBean();
	
	@Override
	public void execute() {
		baseBean.writeLog("=============定时器更新开始==============");
		List<Map<String,String>> list = null;
		list = getNextDate(THRID);
		if(list.size() != 0 || list != null){
			baseBean.writeLog("=============流程前三天提醒==============");
			baseBean.writeLog("================list========="+list);
			createWorkflow(list);
			list.clear();
		}
		list = getNextDate(SECOND);
		if(list.size() != 0 || list != null){
			baseBean.writeLog("=============流程前两天提醒==============");
			baseBean.writeLog("================list========="+list);
			createWorkflow(list);
			list.clear();
		}
		list = getNextDate(FIRST);
		if(list.size() != 0 || list != null){
			baseBean.writeLog("=============流程前一天提醒==============");
			baseBean.writeLog("================list========="+list);
			createWorkflow(list);
			list.clear();
		}
		baseBean.writeLog("=============定时器更新结束==============");
	}
	/**
	 * 获取当前时间后几天的时间
	 * @param day
	 * @return DATE
	 */
	public List<Map<String,String>> getNextDate(int day){
		Calendar calendar = Calendar.getInstance();
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    calendar.add(Calendar.DATE, day);
	    String afterDays = format.format(calendar.getTime());
	    Date date = new Date();
	    String nowDate = format.format(date);
	    List<Map<String,String>> list = new ArrayList<Map<String,String>>();
	    String name = "";		//企画名称
		String subject = "";	//企画阶段
		String creater = "";	//创建人
		String createdate = "";	//创建日期
		String creattime = "";	//创建时间
		String QHid = "";		//企画ID
		String proCode = "";	//企画编码
		String description = "";//提醒描述
		String status = "";
		
	    RecordSet rs = new RecordSet();
	    RecordSet rs1 = new RecordSet();
	    String sql = "SELECT * FROM Prj_TaskProcess WHERE level_n=1 and finish != 100 and enddate='"+afterDays+"'";
		baseBean.writeLog("sql=========="+sql);
	    String sql1 = "";
	    rs.execute(sql);
		while(rs.next()){//结束时间前三天提醒 
			Map<String,String> requestMap = new LinkedHashMap<String,String>();
			QHid = Util.null2String(rs.getString("prjid"));
			subject = Util.null2String(rs.getString("subject"));
			sql1 = "SELECT * FROM Prj_ProjectInfo WHERE id='"+QHid+"'";
			rs1.execute(sql1);
			baseBean.writeLog("企画=========="+sql1);
			if(rs1.next()){
				name = Util.null2String(rs1.getString("name"));
				creater = Util.null2String(rs1.getString("creater"));
				status = Util.null2String(rs1.getString("status"));
			}
			//createdate = Util.null2String(rs1.getString("createdate"));
			//creattime = Util.null2String(rs1.getString("creattime"));
	
			//proCode = Util.null2String(rs1.getString("proCode"));
			requestMap.put("qhmc",name);
			requestMap.put("qhjd",subject);
			requestMap.put("jsr",creater);
			requestMap.put("cjrq",nowDate);
			requestMap.put("qhid",QHid);
			//这里待写
			description = "距  "+subject+" 结束阶段还有  "+day+" 天,请及时关注及填写相应的阶段性报告!";
			requestMap.put("mms",description);
			if("1".equals(status) || "2".equals(status)){
				list.add(requestMap);
			}
		}
		return list;
	}
	
	/**
	 * 创建提醒流程
	 */
	private void createWorkflow(List<Map<String,String>> lists){
		Properties loadTemplateProp = baseBean.LoadTemplateProp("WorkflowCreateByCronJob");
		String creatorid = loadTemplateProp.getProperty("createrid");		//创建人id
		String workflowid = loadTemplateProp.getProperty("workflowid");		//流程id
		String isNextFlow = loadTemplateProp.getProperty("isNextFlow");		//流程是否流转下一节点
		baseBean.writeLog("创建人id============"+creatorid);
		baseBean.writeLog("流程id============="+workflowid);
		baseBean.writeLog("是否流转下一节点============"+isNextFlow);
		String title = "企画阶段提醒";//流程标题
		String requestid = "";
		int ireqid = 0;
		
		
		for(Map<String,String> map : lists){
			requestid = WorkflowCreateByMainTableMap(creatorid, workflowid, title,isNextFlow,map);
			ireqid = Util.getIntValue(requestid, 0);
			if(ireqid > 0){//流程创建成功
				baseBean.writeLog("流程创建成功===================="+requestid);
			}else{
				baseBean.writeLog("流程生成失败===================="+requestid);
			}
		}
		
	}
	/**
	 * 该方法用于生成流程提醒
	 * 
	 * @param createrid		流程创建人
	 * @param workflowid	流程id
	 * @param title			流程标题
	 * @param isNextFlow	是否流转下一节点
	 * @param map			主表字段map
	 * @return
	 */
	public String WorkflowCreateByMainTableMap(String createrid, String workflowid, String title, String isNextFlow, Map<String,String> map)
	{
		String str = "";
		RequestInfo requestinfo = new RequestInfo();
		requestinfo.setCreatorid(createrid);
		requestinfo.setWorkflowid(workflowid);
		requestinfo.setDescription(title);
		if ("0".equals(isNextFlow))
			requestinfo.setIsNextFlow("0");
		MainTableInfo maintableinfo = new MainTableInfo();
		Property aproperty[] = new Property[map.size()];
		int i = 0;
		for (Entry<String, String> entry : map.entrySet()){
			aproperty[i] = new Property();
			aproperty[i].setName(entry.getKey());
			aproperty[i].setValue(entry.getValue());
			System.out.println((new StringBuilder()).append("\r\n---p=").append(i).append("---Key=").append((String)entry.getKey()).append("---Value=").append((String)entry.getValue()).toString());
			baseBean.writeLog((new StringBuilder()).append("\r\n---p=").append(i).append("---Key=").append((String)entry.getKey()).append("---Value=").append((String)entry.getValue()).toString());
			i++;
		}
		baseBean.writeLog("----创建流程参数个数为p:" + i);
		maintableinfo.setProperty(aproperty);
		requestinfo.setMainTableInfo(maintableinfo);
		RequestService requestservice = new RequestService();
		try
		{
			str = requestservice.createRequest(requestinfo);
			StringBuffer stringbuffer = new StringBuffer("\r\n-----创建工作流记录日志开始");
			WorkflowComInfo workflowcominfo = new WorkflowComInfo();
			RequestComInfo requestcominfo = new RequestComInfo();
			ResourceComInfo resourcecominfo = new ResourceComInfo();
			stringbuffer.append((new StringBuilder()).append("\r\n-----姓名:").append(resourcecominfo.getLastname(requestcominfo.getRequestCreater(str))).toString());
			stringbuffer.append((new StringBuilder()).append("\r\n-----时间:").append(requestcominfo.getRequestCreateTime(str)).toString());
			stringbuffer.append((new StringBuilder()).append("\r\n-----创建流程:").append(workflowcominfo.getWorkflowname(workflowid)).toString());
			stringbuffer.append((new StringBuilder()).append("\r\n-----请求:").append(requestcominfo.getRequestname(str)).toString());
			stringbuffer.append((new StringBuilder()).append("\r\n-----请求:").append(str).toString());
			stringbuffer.append("\r\n-----创建工作流日志结束");
			System.out.println((new StringBuilder()).append("").append(stringbuffer.toString()).toString());
		}
		catch (Exception exception)
		{
			baseBean.writeLog("错误====="+(new StringBuilder()).append("").append(exception).toString());
			System.out.println("错误====="+(new StringBuilder()).append("").append(exception).toString());
		}
		return str;
	}
	
}
